package org.nnxy.foodboot.config;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * 缓存配置类
 * 配置Redis缓存，为不同的缓存名称设置不同的过期时间
 */
@Configuration
@EnableCaching
public class CacheConfig {

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        // 默认缓存配置
        RedisCacheConfiguration defaultConfig = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofMinutes(30))  // 默认30分钟过期
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))
                .disableCachingNullValues();  // 不缓存空值

        // 针对不同缓存名称设置不同的过期时间
        Map<String, RedisCacheConfiguration> configMap = new HashMap<>();
        
        // 分布统计数据缓存时间较长，1小时
        configMap.put("categoryDistribution", defaultConfig.entryTtl(Duration.ofHours(1)));
        configMap.put("flavorDistribution", defaultConfig.entryTtl(Duration.ofHours(1)));
        configMap.put("techniqueDistribution", defaultConfig.entryTtl(Duration.ofHours(1)));
        configMap.put("difficultyDistribution", defaultConfig.entryTtl(Duration.ofHours(1)));

        // 趋势数据更新较频繁，15分钟
        configMap.put("browseTrend", defaultConfig.entryTtl(Duration.ofMinutes(15)));
        configMap.put("favoriteTrend", defaultConfig.entryTtl(Duration.ofMinutes(15)));
        configMap.put("recommendationTrend", defaultConfig.entryTtl(Duration.ofMinutes(15)));

        // 热门排行数据，20分钟
        configMap.put("mostViewedRecipes", defaultConfig.entryTtl(Duration.ofMinutes(20)));
        configMap.put("mostFavoritedRecipes", defaultConfig.entryTtl(Duration.ofMinutes(20)));
        configMap.put("highestRatedRecipes", defaultConfig.entryTtl(Duration.ofMinutes(20)));
        configMap.put("recentRecipes", defaultConfig.entryTtl(Duration.ofMinutes(20)));

        // 总览统计数据，30分钟
        configMap.put("recipeStatistics", defaultConfig.entryTtl(Duration.ofMinutes(30)));
        configMap.put("interactionStatistics", defaultConfig.entryTtl(Duration.ofMinutes(30)));
        configMap.put("popularRecipes", defaultConfig.entryTtl(Duration.ofMinutes(30)));
        
        // 自定义范围查询，10分钟
        configMap.put("customRangeStats", defaultConfig.entryTtl(Duration.ofMinutes(10)));
        
        // 月度统计数据，2小时
        configMap.put("monthlyStats", defaultConfig.entryTtl(Duration.ofHours(2)));
        
        // 详情统计数据，1小时
        configMap.put("recipeDetailStats", defaultConfig.entryTtl(Duration.ofHours(1)));

        return RedisCacheManager.builder(connectionFactory)
                .cacheDefaults(defaultConfig)
                .withInitialCacheConfigurations(configMap)
                .build();
    }
} 